package com.sweets.look.ui.setting;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sweet.satellite.GlobalConfig;
import com.sweet.satellite.TranslucentSBActivity;
import com.sweets.look.R;
import com.sweets.look.preference.PreferenceManager;
import com.sweets.look.ui.widget.MarkRadioDialog;
import com.sweets.look.util.ViewUtils;

/**
 * 高级设置
 */
public class AdvancedSettings extends TranslucentSBActivity {
    private CheckBox mCbCompatibleMode;
    private CheckBox mCbCookies;
    private CheckBox mCbIncognitoCookies;
    private CheckBox mCbIntent;
    private CheckBox mCbJava;
    private CheckBox mCbRestore;
    private CheckBox mCbWindow;
    private Context mContext;
    private TextView mEncodeText;
    private PreferenceManager mPreferences;


    private void initCompatibleModePreference() {
        this.mCbCompatibleMode = (CheckBox) this.findViewById(R.id.cbCompatibleMode);
        this.mCbCompatibleMode.setChecked(this.mPreferences.getQuickBackEnabled());
        this.mCbCompatibleMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener
                () {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setQuickBackEnabled(isChecked);
            }
        });
    }

    private void initCooliesPreference() {
        this.mCbCookies = (CheckBox) this.findViewById(R.id.cbCookies);
        this.mCbCookies.setChecked(this.mPreferences.getCookiesEnabled());
        this.mCbCookies.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setCookiesEnabled(isChecked);
            }
        });
    }

    private void initEncodeText() {
        switch (this.mPreferences.getTextEncodingChoice()) {
            case 0: {
                this.mEncodeText.setText(this.getResources().getString(R.string.encode_utf_8));
                return;
            }
            case 1: {
                this.mEncodeText.setText(this.getResources().getString(R.string.encode_gbk));
                return;
            }
            case 2: {
                this.mEncodeText.setText(this.getResources().getString(R.string.encode_big_5));
                return;
            }
            case 3: {
                this.mEncodeText.setText(this.getResources().getString(R.string.encode_iso_8859_1));
                return;
            }
            case 4: {
                this.mEncodeText.setText(this.getResources().getString(R.string.encode_iso_2022_jp));
                return;
            }
        }
    }

    private void initIncognitoCookiesPreference() {
        this.mCbIncognitoCookies = (CheckBox) this.findViewById(R.id.cbIncognitoCookies);
        this.mCbIncognitoCookies.setChecked(this.mPreferences.getIncognitoCookiesEnabled());
        this.mCbIncognitoCookies.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener
                () {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setIncognitoCookiesEnabled(isChecked);
            }
        });
    }

    private void initIntentPreference() {
        this.mCbIntent = (CheckBox) this.findViewById(R.id.cbIntent);
        this.mCbIntent.setChecked(this.mPreferences.getWebIntentEnabled());
        this.mCbIntent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setWebIntentEnabled(isChecked);
            }
        });
    }

    @SuppressLint(value = {"SetJavaScriptEnabled"})
    private void initJavaPreference() {
        this.mCbJava = (CheckBox) this.findViewById(R.id.cbJava);
        this.mCbJava.setChecked(this.mPreferences.getJavaScriptEnabled());
        this.mCbJava.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setJavaScriptEnabled(isChecked);
            }
        });
    }

    private void initRestorePreference() {
        this.mCbRestore = (CheckBox) this.findViewById(R.id.cbRestore);
        this.mCbRestore.setChecked(this.mPreferences.getRestoreLostTabsEnabled());
        this.mCbRestore.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setRestoreLostTabsEnabled(isChecked);
            }
        });
    }

    private void initWindowPreference() {
        this.mCbWindow = (CheckBox) this.findViewById(R.id.cbWindow);
        this.mCbWindow.setChecked(this.mPreferences.getPopupsEnabled());
        this.mCbWindow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mPreferences.setPopupsEnabled(isChecked);
            }
        });
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.settings_advanced);
        this.setToolbarTitle(R.string.settings_advanced);
        this.mContext = this;
        this.mPreferences = PreferenceManager.getInstance();
        this.mEncodeText = (TextView) this.findViewById(R.id.encodeText);
        this.initEncodeText();
        this.initJavaPreference();
        this.initWindowPreference();
        this.initCooliesPreference();
        this.initIncognitoCookiesPreference();
        this.initIntentPreference();
        this.initRestorePreference();
        this.initCompatibleModePreference();
        ViewUtils.swipeToFinish(this.findViewById(R.id.settingLayout), this);
    }

    public void onSettingsItemClick(View v) {

        boolean v0 = true;
        switch (v.getId()) {
            case R.id.layoutTextEncoding: {
                new MarkRadioDialog(this.mContext).builder()
                        .setTitle(this.getResources().getString(
                                R.string.text_encoding)).setCancelable(true)
                        .setSingleChoiceItems(R.array.array_text_encoding, this.mPreferences
                                .getTextEncodingChoice(), new RadioGroup.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                mPreferences.setTextEncodingChoice(checkedId);
                                initEncodeText();
                            }
                        }).show();
                break;
            }
            case R.id.layoutJava: {

                if (this.mCbJava.isChecked()) {
                    v0 = false;
                }

                mCbJava.setChecked(v0);
                break;
            }
            case R.id.layoutWindow: {

                if (this.mCbWindow.isChecked()) {
                    v0 = false;
                }

                mCbWindow.setChecked(v0);
                break;
            }
            case R.id.layoutCookies: {

                if (this.mCbCookies.isChecked()) {
                    v0 = false;
                }

                mCbCookies.setChecked(v0);
                break;
            }
            case R.id.layoutIncognitoCookies: {

                if (this.mCbIncognitoCookies.isChecked()) {
                    v0 = false;
                }

                mCbIncognitoCookies.setChecked(v0);
                break;
            }
            case R.id.layoutIntent: {

                if (this.mCbIntent.isChecked()) {
                    v0 = false;
                }

                mCbIntent.setChecked(v0);
                break;
            }
            case R.id.layoutRestore: {

                if (this.mCbRestore.isChecked()) {
                    v0 = false;
                }

                mCbRestore.setChecked(v0);
                break;
            }
            case R.id.layoutCompatibleMode: {

                if (this.mCbCompatibleMode.isChecked()) {
                    v0 = false;
                }

                mCbCompatibleMode.setChecked(v0);
                break;
            }
        }
    }

}

